package com.fr.report.io;

import com.fr.base.BaseUtils;
import com.fr.base.FRFont;
import com.fr.base.Painter;
import com.fr.base.Style;
import com.fr.base.background.Background;
import com.fr.base.background.ColorBackground;
import com.fr.base.core.GraphHelper;
import com.fr.data.core.db.BinaryObject;
import com.fr.report.CellElement;
import com.fr.report.PageSet;
import com.fr.report.PageSetChain;
import com.fr.report.ReportPage;
import com.fr.report.ReportSettings;
import com.fr.report.ResultWorkBook;
import com.fr.report.cellElement.Formula;
import com.fr.report.core.DynamicValueList;
import com.fr.report.core.PaintUtils;
import com.fr.report.core.ReportHelper;
import com.fr.report.core.ReportUtils;
import com.fr.third.com.lowagie.text.Cell;
import com.fr.third.com.lowagie.text.Document;
import com.fr.third.com.lowagie.text.Element;
import com.fr.third.com.lowagie.text.Font;
import com.fr.third.com.lowagie.text.Paragraph;
import com.fr.third.com.lowagie.text.Rectangle;
import com.fr.third.com.lowagie.text.Row;
import com.fr.third.com.lowagie.text.Table;
import com.fr.third.com.lowagie.text.rtf.RtfWriter2;
import com.fr.third.com.lowagie.text.rtf.style.RtfFont;
import com.fr.third.com.lowagie.text.rtf.table.RtfBorderGroup;
import com.fr.third.com.lowagie.text.rtf.table.RtfCell;
import com.fr.util.Utils;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Clob;
import java.util.Iterator;

public class WordExporter extends AbstractAppExporter
{
  public void export(OutputStream paramOutputStream, ResultWorkBook paramResultWorkBook)
    throws Exception
  {
    PageSet localPageSet = paramResultWorkBook.generateReportPageSet(ReportUtils.getPaperSettingListFromWorkBook(paramResultWorkBook)).getTraversingCase();
    export(paramOutputStream, localPageSet);
    localPageSet.release();
  }

  public void export(OutputStream paramOutputStream, PageSet paramPageSet)
    throws Exception
  {
    Document localDocument = new Document();
    RtfWriter2.getInstance(localDocument, paramOutputStream);
    localDocument.open();
    float f1 = 0.75F;
    for (int i = 0; i < paramPageSet.size(); ++i)
    {
      Object localObject1;
      Object localObject3;
      ReportPage localReportPage = paramPageSet.getPage(i);
      if (localReportPage == null)
        break;
      if (i != 0)
        localDocument.newPage();
      float f2 = localReportPage.getPageWidth() * f1;
      float f3 = localReportPage.getPageHeight() * f1;
      Rectangle localRectangle = new Rectangle(f2, f3);
      localDocument.setPageSize(localRectangle);
      localDocument.setMargins(localReportPage.getMarginLeft() * f1, localReportPage.getMarginRight() * f1, localReportPage.getMarginTop() * f1, localReportPage.getMarginBottom() * f1);
      int j = localReportPage.getColumnCount();
      int k = localReportPage.getRowCount();
      Table localTable = new Table(Math.max(1, j), Math.max(1, k));
      localTable.setBorder(0);
      ReportSettings localReportSettings = localReportPage.getReportSettings();
      Background localBackground = localReportSettings.getBackground();
      Color localColor = Color.white;
      if ((localBackground != null) && (localBackground instanceof ColorBackground))
      {
        localColor = ((ColorBackground)localBackground).getColor();
        localTable.setBackgroundColor(localColor);
      }
      localTable.setAlignment(4);
      Style localStyle1 = Style.DEFAULT_STYLE;
      localTable.setDefaultCell(createDefaultCell(localColor, localStyle1));
      localTable.setAutoFillEmptyCells(true);
      localTable.setPadding(0.0F);
      localTable.setSpacing(0.0F);
      float[] arrayOfFloat = new float[j];
      float f4 = 0.0F;
      for (int l = 0; l < j; ++l)
      {
        arrayOfFloat[l] = (localReportPage.getColumnWidth(l) * f1);
        f4 += arrayOfFloat[l];
      }
      float f5 = localDocument.getPageSize().getWidth() - localDocument.leftMargin() - localDocument.rightMargin();
      localTable.setWidth(f4 / f5 * 100.0F);
      localTable.setWidths(arrayOfFloat);
      Iterator localIterator = localReportPage.cellIterator();
      DynamicValueList localDynamicValueList1 = ReportHelper.createColumnWidthList(localReportPage);
      DynamicValueList localDynamicValueList2 = ReportHelper.createRowHeightList(localReportPage);
      while (true)
      {
        Object localObject2;
        do
        {
          do
          {
            do
            {
              if (!(localIterator.hasNext()))
                break label699;
              CellElement localCellElement = (CellElement)localIterator.next();
              i2 = localCellElement.getRow();
              i3 = localCellElement.getColumn();
              Cell localCell = createCellFromCellElement(localCellElement, localDynamicValueList1, localDynamicValueList2);
              if ((localCell != null) && (localDynamicValueList1.getRangeValue(i3, i3 + localCellElement.getColumnSpan()) != 0) && (localDynamicValueList2.getRangeValue(i2, i2 + localCellElement.getRowSpan()) != 0))
              {
                int i5 = i2 + localCellElement.getRowSpan();
                while (i2 < i5)
                {
                  if (localDynamicValueList2.get(i2) > 0)
                    break;
                  ++i2;
                }
                localTable.addCell(localCell, i2, i3);
              }
              localObject1 = localCellElement.getStyle();
            }
            while (localObject1 == null);
            if ((((Style)localObject1).getBorderBottom() != 0) && (i2 + 1 < k))
            {
              localObject2 = localTable.getElement(i2 + 1, i3);
              if (localObject2 instanceof RtfCell)
              {
                localObject3 = (RtfCell)localObject2;
                localStyle2 = Style.DEFAULT_STYLE;
                localStyle2 = localStyle2.deriveBorderTop(((Style)localObject1).getBorderBottom(), ((Style)localObject1).getBorderBottomColor());
                applyStyleToRtfCell((RtfCell)localObject3, localStyle2);
              }
            }
          }
          while ((((Style)localObject1).getBorderRight() == 0) || (i3 + 1 >= j));
          localObject2 = localTable.getElement(i2, i3 + 1);
        }
        while (!(localObject2 instanceof RtfCell));
        localObject3 = (RtfCell)localObject2;
        Style localStyle2 = Style.DEFAULT_STYLE;
        localStyle2 = localStyle2.deriveBorderLeft(((Style)localObject1).getBorderRight(), ((Style)localObject1).getBorderRightColor());
        applyStyleToRtfCell((RtfCell)localObject3, localStyle2);
      }
      for (int i1 = localReportPage.getColumnCount() - 1; i1 >= 0; --i1)
        if (localReportPage.getColumnWidth(i1) == 0)
          localTable.deleteColumn(i1);
      for (i1 = localTable.size() - 1; i1 >= localReportPage.getRowCount(); --i1)
        localTable.deleteRow(i1);
      float f6 = 0.0F;
      int i2 = 0;
      int i3 = localReportPage.getRowCount() - 1;
      for (int i4 = localReportPage.getRowCount() - 1; i4 >= 0; --i4)
        if (localReportPage.getRowHeight(i4) == 0)
        {
          localTable.deleteRow(i4);
          --i3;
        }
        else
        {
          localObject1 = localTable.getRow(i4);
          ((Row)localObject1).setHeight(localReportPage.getRowHeight(i4) * -1 * 15);
          f6 += localReportPage.getRowHeight(i4);
          if (localReportPage.getRowHeight(i4) > i2)
          {
            i2 = localReportPage.getRowHeight(i4);
            i3 = i4;
          }
        }
      float f7 = localDocument.getPageSize().getHeight() - localDocument.topMargin() - localDocument.bottomMargin();
      float f8 = (f1 * f6 - f7 + 2.0F) / f1;
      if (f8 > 0.0F)
      {
        int i6 = (int)f8;
        if (f8 - i6 > 0.0F)
          ++i6;
        localObject3 = localTable.getRow(i3);
        ((Row)localObject3).setHeight((i2 - i6) * -1 * 15);
      }
      localDocument.add(localTable);
    }
    if (localDocument != null)
      label699: localDocument.close();
  }

  private Cell createDefaultCell(Color paramColor, Style paramStyle)
    throws Exception
  {
    RtfCell localRtfCell = new RtfCell(new Paragraph("", frFont2RTFFont(paramStyle.getFRFont())));
    localRtfCell.setBackgroundColor(paramColor);
    RtfBorderGroup localRtfBorderGroup = new RtfBorderGroup();
    localRtfBorderGroup.addBorder(1, 1, 1.0F, paramColor);
    localRtfBorderGroup.addBorder(8, 1, 1.0F, paramColor);
    localRtfBorderGroup.addBorder(2, 1, 1.0F, paramColor);
    localRtfBorderGroup.addBorder(4, 1, 1.0F, paramColor);
    localRtfCell.setBorders(localRtfBorderGroup);
    return localRtfCell;
  }

  private Cell createCellFromCellElement(CellElement paramCellElement, DynamicValueList paramDynamicValueList1, DynamicValueList paramDynamicValueList2)
    throws Exception
  {
    label213: RtfCell localRtfCell;
    Object localObject2;
    Object localObject3;
    Object localObject4;
    Style localStyle = paramCellElement.getStyle();
    Object localObject1 = paramCellElement.getValue();
    if (localObject1 instanceof Painter)
    {
      int i = paramDynamicValueList1.getRangeValue(paramCellElement.getColumn(), paramCellElement.getColumn() + paramCellElement.getColumnSpan());
      int k = paramDynamicValueList2.getRangeValue(paramCellElement.getRow(), paramCellElement.getRow() + paramCellElement.getRowSpan());
      i = (int)(i * 9.65D / 10.0D);
      localObject4 = GraphHelper.createBufferedImage(i, k, 2);
      Graphics2D localGraphics2D = ((BufferedImage)localObject4).createGraphics();
      ((Painter)localObject1).paint(localGraphics2D, i, k, localStyle);
      localGraphics2D.dispose();
      localObject1 = localObject4;
    }
    else if (localObject1 instanceof Clob)
    {
      localObject1 = Utils.clob2String((Clob)localObject1);
    }
    else if (localObject1 instanceof BinaryObject)
    {
      localObject2 = (BinaryObject)localObject1;
      localObject3 = null;
      if (((BinaryObject)localObject2).getBytes().length >= 5000000)
        if ((localObject3 = ((BinaryObject)localObject2).getImage()) == null)
          break label213;
      localObject1 = (localObject3 == null) ? ((BinaryObject)localObject2).getValue() : localObject3;
    }
    if (localObject1 instanceof java.awt.Image)
    {
      localObject2 = new ByteArrayOutputStream();
      localObject3 = (java.awt.Image)localObject1;
      BaseUtils.writeImage((java.awt.Image)localObject3, "png", (OutputStream)localObject2);
      localObject4 = com.fr.third.com.lowagie.text.Image.getInstance(((ByteArrayOutputStream)localObject2).toByteArray());
      float f1 = ((java.awt.Image)localObject3).getWidth(null);
      float f2 = ((java.awt.Image)localObject3).getHeight(null);
      ((com.fr.third.com.lowagie.text.Image)localObject4).scaleAbsolute(f1 * 72.0F, f2 * 72.0F);
      localRtfCell = new RtfCell((Element)localObject4);
    }
    else
    {
      if (localObject1 instanceof Formula)
        localObject1 = ((Formula)localObject1).getResult();
      localObject2 = PaintUtils.valueToText(localObject1, localStyle.getFormat());
      localObject2 = ((String)localObject2).replaceAll("\\r", "");
      localObject3 = new Paragraph((String)localObject2, frFont2RTFFont(localStyle.getFRFont()));
      ((Paragraph)localObject3).setSpacingBefore(localStyle.getSpacingBefore() * 0.5F);
      ((Paragraph)localObject3).setSpacingAfter(localStyle.getSpacingAfter() * 0.5F);
      ((Paragraph)localObject3).setLeading(localStyle.getLineSpacing() * 2.0F);
      localRtfCell = new RtfCell((Element)localObject3);
    }
    if (localRtfCell != null)
    {
      int j = paramCellElement.getRowSpan();
      for (int l = paramCellElement.getRow(); l < paramCellElement.getRow() + paramCellElement.getRowSpan(); ++l)
        if (paramDynamicValueList2.get(l) == 0)
          --j;
      localRtfCell.setColspan(paramCellElement.getColumnSpan());
      localRtfCell.setRowspan(j);
      applyStyleToRtfCell(localRtfCell, localStyle);
    }
    return ((Cell)(Cell)(Cell)(Cell)localRtfCell);
  }

  private void applyStyleToRtfCell(RtfCell paramRtfCell, Style paramStyle)
  {
    Background localBackground = paramStyle.getBackground();
    Color localColor = Color.white;
    if (localBackground instanceof ColorBackground)
    {
      paramRtfCell.setBackgroundColor(((ColorBackground)localBackground).getColor());
      localColor = ((ColorBackground)localBackground).getColor();
    }
    RtfBorderGroup localRtfBorderGroup = new RtfBorderGroup();
    if (paramStyle.getBorderTop() != 0)
      localRtfBorderGroup.addBorder(1, frBorder2RTFBorder(paramStyle.getBorderTop()), frBorderWidth2RTFBorderWidth(paramStyle.getBorderTop()), paramStyle.getBorderTopColor());
    else
      localRtfBorderGroup.addBorder(1, 1, 0.5F, localColor);
    if (paramStyle.getBorderLeft() != 0)
      localRtfBorderGroup.addBorder(4, frBorder2RTFBorder(paramStyle.getBorderLeft()), frBorderWidth2RTFBorderWidth(paramStyle.getBorderLeft()), paramStyle.getBorderLeftColor());
    else
      localRtfBorderGroup.addBorder(4, 1, 0.5F, localColor);
    if (paramStyle.getBorderBottom() != 0)
      localRtfBorderGroup.addBorder(2, frBorder2RTFBorder(paramStyle.getBorderBottom()), frBorderWidth2RTFBorderWidth(paramStyle.getBorderBottom()), paramStyle.getBorderBottomColor());
    else
      localRtfBorderGroup.addBorder(2, 1, 0.5F, localColor);
    if (paramStyle.getBorderRight() != 0)
      localRtfBorderGroup.addBorder(8, frBorder2RTFBorder(paramStyle.getBorderRight()), frBorderWidth2RTFBorderWidth(paramStyle.getBorderRight()), paramStyle.getBorderRightColor());
    else
      localRtfBorderGroup.addBorder(8, 1, 0.5F, localColor);
    paramRtfCell.setBorders(localRtfBorderGroup);
    int i = paramStyle.getHorizontalAlignment();
    if (i == 2)
      paramRtfCell.setHorizontalAlignment(0);
    else if (i == 0)
      paramRtfCell.setHorizontalAlignment(1);
    else if (i == 4)
      paramRtfCell.setHorizontalAlignment(2);
    int j = paramStyle.getVerticalAlignment();
    if (j == 1)
      paramRtfCell.setVerticalAlignment(4);
    else if (i == 0)
      paramRtfCell.setVerticalAlignment(5);
    else if (i == 3)
      paramRtfCell.setVerticalAlignment(6);
  }

  private int frBorder2RTFBorder(int paramInt)
  {
    switch (paramInt)
    {
    case 0:
      return 0;
    case 3:
    case 14:
      return 5;
    case 9:
      return 8;
    case 11:
      return 9;
    case 7:
      return 4;
    case 6:
      return 7;
    case 15:
      return 7;
    case 4:
      return 4;
    case 5:
      return 1;
    case 1:
      return 1;
    case 2:
    case 8:
    case 10:
    case 12:
    case 13:
    }
    return 1;
  }

  private float frBorderWidth2RTFBorderWidth(int paramInt)
  {
    return GraphHelper.getLineStyleSize(paramInt);
  }

  private Font frFont2RTFFont(FRFont paramFRFont)
  {
    int i;
    switch (paramFRFont.getStyle())
    {
    case 0:
      i = 0;
      break;
    case 1:
      i = 1;
      break;
    case 2:
      i = 2;
      break;
    case 3:
      i = 3;
      break;
    default:
      i = 0;
    }
    if (paramFRFont.isShadow())
      i += 32;
    if (paramFRFont.isStrikethrough())
      i += 8;
    if (paramFRFont.getUnderline() != 0)
      i += 4;
    String str = paramFRFont.getFamily();
    int j = 0;
    if ("仿宋_GB2312".equals(str))
      try
      {
        str = new String("仿宋_GB2312".getBytes("gb18030"), "iso-8859-1");
        j = 134;
      }
      catch (UnsupportedEncodingException localUnsupportedEncodingException)
      {
      }
    RtfFont localRtfFont = new RtfFont(str, paramFRFont.getSize() * 0.75F, i, paramFRFont.getForeground(), j);
    return localRtfFont;
  }
}